<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | Markdown v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace common;

class Markdown {

    /**
     * 生成加粗markdown文本
     *
     * @param $text
     * @return string
     */
    public static function getBoldText($text) {
        return "**" . $text . "**";
    }

    /**
     * 生成倾斜markdown文本
     *
     * @param $text
     * @return string
     */
    public static function getItalicText($text) {
        return "*" . $text . "*";
    }

    /**
     * 生成链接markdown文本
     *
     * @param $text
     * @param $href
     * @return string
     */
    public static function getLinkText($text, $href) {
        return "[" . $text . "](" . $href . ")";
    }

    /**
     * 生成图片markdown文本
     *
     * @param $imageUrl
     * @return string
     */
    public static function getImageText($imageUrl) {
        return "![image](" . $imageUrl . ")";
    }

    /**
     * 生成标题markdown文本
     *
     * @param $headerType
     * @param $text
     * @return string
     */
    public static function getHeaderText($headerType, $text) {
        $headerType = intval($headerType);
        if ($headerType < 1 || $headerType > 6) {
            return $text;
        }

        $numbers = "";
        for ($i = 0; $i < $headerType; $i++) {
            $numbers .= "#";
        }
        return $numbers . " " . $text;
    }

    /**
     * 生成引用markdown文本
     *
     * @param $text
     * @return string
     */
    public static function getReferenceText($text) {
        return "> " . $text;
    }

    /**
     * 生成有序列表markdown文本
     *
     * @param array $orderItems
     * @return string
     */
    public static function getOrderListText(array $orderItems) {
        if (empty($orderItems)) {
            return "";
        }

        $text = "";
        $idx = 1;
        foreach ($orderItems as $orderItem) {
            if (!empty($text)) {
                $text .= "\n";
            }
            $text .= $idx . ". " . $orderItem;
            $idx++;
        }
        return $text;
    }

    /**
     * 生成无序列表markdown文本
     *
     * @param $unorderItems
     * @return string
     */
    public static function getUnorderListText($unorderItems) {
        if (empty($unorderItems)) {
            return "";
        }

        $text = "";
        foreach ($unorderItems as $unorderItem) {
            if (!empty($text)) {
                $text .= "\n";
            }
            $text .= "- " . $unorderItem;
        }
        return $text;
    }	
}

